package demo.controller;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/swagger-ui")
public class SwaggerUIController 
{
	private final static String SWAGGER_JSON_DIR="/generated/swagger-ui/";
	@ResponseBody
	@RequestMapping("/swaggerJsonURL")
	public Object getSwaggerJsonURLs(HttpServletRequest request)
	{
		Map<String,String> urls=new HashMap<String, String>();
		File dir=new File(request.getRealPath("/")+SWAGGER_JSON_DIR);
		if(!dir.exists())
			return urls;
		getURLs(urls,dir,request.getContextPath()+SWAGGER_JSON_DIR);
		return urls;
	}
	
	private void getURLs(Map<String,String> urls,File dir,String prefix)
	{
		if(!dir.exists())
			return ;
		File[] files=dir.listFiles();
		for(File temp:files)
			if(temp.isDirectory())
				getURLs(urls,temp,prefix+temp.getName()+"/");
			else
				urls.put(temp.getName(), prefix+temp.getName());
	}
}
